import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import {  HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import {Service} from '../../../server/server';
import {InitService} from '../../../public/serve/init/init.service';
import { keywordTableAddtype } from '../keywordtable.data'
import {DatePipe} from '@angular/common';
import {CudTableService} from '../../../public/serve/cud-table/cud-table.service';
import {HintsService} from '../../../public/serve/hints/hints.service';

declare var $: any;

interface keywordTableAddResponse {
    result: string[];
    result_code: string;
    message: string;
}


@Component({
  selector: 'app-keyword-table-add',
  templateUrl: './keyword-table-add.component.html',
})

export class KeywordTableAddComponent implements OnInit {
  constructor(private hintsService: HintsService,private http: HttpClient, private router: Router, private server:Service,public initService: InitService, private datePipe: DatePipe, private tableListService: CudTableService) {
  }

  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();
  //清空
  @ViewChild('addForm') formElement: HTMLFormElement;
  public parentList : any;
  @Input() details: any;
  @Input() themeTableTagListVal: any;

  reset() {
    this.initService._init();
    $("#add").modal("hide");
    // this.formElement.reset();
    setTimeout(() => {
      this.default();
    }, 300);
  }

  //默认数据
  default() {
    this.keywordTableAdd.themeGroupId = 0;
    this.keywordTableAdd.name = '';
    this.keywordTableAdd.checkStatus = 0;
    this.keywordTableAdd.status = 0;
    this.keywordTableAdd.description = '';
    this.keywordTableAdd.labelName = '';
    this.keywordTableAdd.labelId = '';
  }

  _showAddChildInfo(){
    this.details = '';
  }

  //datatype
  keywordTableAdd: keywordTableAddtype = {
    parentId: '',
    child: 0,
    name: '',
    createTime: '',
    status: 0,
    checkStatus: 0,
    create_by :  '',
    description: '',
    themeGroupId: 0,
    themeTableId:'',
    labelId:'',
    labelName:'',
    themeTableCode:''
  };

  //用户id
  createUserId: any;

  ngOnInit() {
    this.getParentList();
  }

    public getParentList() {
        let url = this.server.baseUrl + 'Service/ThemeWords/getThemeTablesList';
        this.tableListService.postRequest(url, {}).then((data: any) => {
            if(data.result_code == '0000') {
                this.parentList = data.result;
            }
        }).catch();
    }

    //add
    add(v): void {

      let parentId = Number(this.keywordTableAdd.parentId);
      if(this.details){
        parentId = Number(this.details['themeTableId']);
      }

        let body = {
            child: Number(this.keywordTableAdd.child),
            createBy: this.server.getuserName(),
            themeTableId: '',
            description: this.keywordTableAdd.description,
            name: v['wordName'],
            parentId: parentId,
            status: Number(this.keywordTableAdd.status),
            checkStatus: Number(this.keywordTableAdd.checkStatus),
            themeGroupId: Number(this.keywordTableAdd.themeGroupId),
            labelId: v['labelId'],
            themeTableCode: v['themeTableCode']
        };

    this.http.post<keywordTableAddResponse>(this.server.baseUrl + 'Service/ThemeWords/addThemeTable', body)
      .subscribe(res => {
        this.addOutput.emit(res.result_code);
        this._showAddChildInfo();
        //console.log(res)
        if (res.result_code === '0000') {
          this.reset();
        }
        this.hintsService.messages(res.message, res.result_code);
        this._initAdd();
      })
  }

  /**
    * 初始化
    */
   private _initAdd(){
    this.initService._init();
    this.formElement.reset();
    $("#add").modal("hide");
   }

   _checkNullObj (obj) {
      for (let i in obj) { // 如果不为空，则会执行到这一步，返回true
          return true;
      }
      return false;
   }
}
